#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
from django.conf import settings
from django.core.wsgi import get_wsgi_application
from django.core.management import execute_from_command_line
import os


class DJSettings:
    def __init__(self, INSTALLED_APPS=[], **kwargs) -> None:
        self._INSTALLED_APPS = INSTALLED_APPS
        _default = dict(DATABASES={
            'default': {
                'ENGINE': 'django.db.backends.sqlite3',
                'NAME': 'sqlite.db',
            }
        }, DEBUG=True)
        _default.update(kwargs)
        self.__startapp__()
        _default.update({"INSTALLED_APPS": self._INSTALLED_APPS})
        settings.configure(**_default)
        get_wsgi_application()

    def __startapp__(self):
        for app_ in self._INSTALLED_APPS:
            if not os.path.exists(app_):
                execute_from_command_line(["", "startapp", app_])

    def migrate(self):
        execute_from_command_line(["", "migrate"])
        return self

    def makemigrations(self):
        execute_from_command_line(["", "makemigrations"])
        return self

    def sync_db(self):
        self.makemigrations()
        self.migrate()
        return self


def main():
    """Run administrative tasks."""
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()
